package com.ocean.core.util;

import com.ocean.cache.redis.IRedisCache;
import com.ocean.core.util.constants.ConstantsCache;
import com.ocean.core.util.constants.ConstantsOther;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.*;

/**
 * 判断是否为空
 *
 * @author zhang
 * @version 1.0
 * @date 2020-7-28
 */
@Slf4j
@Component
public class StringUtil {

    private static IRedisCache iRedisCache;

    public StringUtil(IRedisCache iRedisCache) {
        StringUtil.iRedisCache = iRedisCache;
    }

    /**
     * 判断不为空
     *
     * @param value value
     * @return boolean
     */
    public static boolean isNotNull(String value) {
        return null != value && value.length() != 0 && !"null".equals(value);
    }

    /**
     * 判断不为空
     *
     * @param value value
     * @return boolean
     */
    public static boolean isNotNull(Object... value) {
        if (value != null) {
            for (Object s : value) {
                if (isNotNull(s)) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 判断不为空
     *
     * @param value value
     * @return boolean
     */
    public static boolean isNotNull(Object value) {
        return null != value;
    }

    /**
     * 判断不为空
     *
     * @param value value
     * @return boolean
     */
    public static boolean isNotNull(Integer value) {
        return null != value;
    }

    /**
     * 判断不为空
     *
     * @param value value
     * @return boolean
     */
    public static boolean isNotNull(Long value) {
        return null != value && 0L != value;
    }

    /**
     * 判断不为空
     *
     * @param collection 集合
     * @return boolean
     */
    public static boolean isNotNull(Collection<?> collection) {
        return collection != null && collection.size() != 0;
    }

    /**
     * 判断为空
     *
     * @param value value
     * @return 结果
     */
    public static boolean isNull(Integer value) {
        return null == value;
    }

    /**
     * 判断为空
     *
     * @param value value
     * @return 结果
     */
    public static boolean isNull(Object value) {
        return null == value;
    }

    /**
     * 判断为空
     *
     * @param value value
     * @return 结果
     */
    public static boolean isNull(long value) {
        return value == 0;
    }

    /**
     * 判断为空
     *
     * @param value value
     * @return boolean
     */
    public static boolean isNull(String value) {
        return value == null || value.length() <= 0;
    }

    /**
     * 判断为空
     *
     * @param bigDecimal BigDecimal
     * @return boolean
     */
    public static boolean isNull(BigDecimal bigDecimal) {
        return BigDecimal.ZERO.equals(bigDecimal);
    }

    /**
     * 判断为空
     *
     * @param collection 集合
     * @return boolean
     */
    public static boolean isNull(Collection<?> collection) {
        return collection == null || collection.size() == 0;
    }

    /**
     * 将字符串转为List，依据为,
     *
     * @return List<String>
     */
    public static List<String> stringConversionList(String res) throws IllegalArgumentException {
        try {
            List<String> array = new ArrayList<>();
            if (isNotNull(res)) {
                String[] dataset = res.split(",");
                array.addAll(Arrays.asList(dataset));
                return array;
            }
        } catch (Exception e) {
            throw new IllegalArgumentException("参数非法...");
        }
        return Collections.emptyList();
    }

    /**
     * 数组转字符串
     *
     * @param objects 需要转换的对象
     * @return 字符串
     */
    public static String arrayConversionString(Object[] objects) {
        if (objects.length != 0) {
            return Arrays.toString(objects).replace("[", "").replace("]", "");
        }
        return null;
    }

    /**
     * 生成1-100的随机数
     *
     * @return 结果
     */
    public static Integer random() {
        return (int) (Math.random() * (100 - 1) + 1);
    }

    /**
     * 生成范围内随机数
     *
     * @param min 最小值
     * @param max 最大值
     * @return 随机数
     */
    public static Integer random(int min, int max) {
        return (int) (min + Math.random() * max);
    }

    /**
     * 生成6位随机数字
     *
     * @return 结果
     */
    public static int randomInt() {
        return (int) (10000 + Math.random() * 990000);
    }

    /**
     * 字符串转String数组
     *
     * @param target 目标字符
     * @return 结果
     */
    public static String[] stringCoverArray(String target) {
        return target.split(",");
    }

    /**
     * 随机生成汉字
     *
     * @return char字符
     */
    public static char randomCharacter() {
        String str = "";
        int highPos;
        int lowPos;
        Random random = new Random();
        highPos = (176 + Math.abs(random.nextInt(39)));
        lowPos = (161 + Math.abs(random.nextInt(93)));
        byte[] b = new byte[2];
        b[0] = (Integer.valueOf(highPos)).byteValue();
        b[1] = (Integer.valueOf(lowPos)).byteValue();
        try {
            str = new String(b, "GBK");
        } catch (UnsupportedEncodingException e) {
            log.error("生成汉字时遇到错误：" + e.getMessage());
        }
        return str.charAt(0);
    }

    /**
     * 字符串转int数组
     *
     * @param target 目标字符
     * @return 结果
     */
    public static int[] getIntArray(String target) {
        char[] chars = target.toCharArray();
        int[] ret = new int[chars.length];
        for (int i = 0; i < chars.length; i++) {
            ret[i] = chars[i] - 48;
        }
        return ret;
    }

    /**
     * 生成7位数编号
     *
     * @return 返回一个新的报告卡编号
     */
    public static String generateBgkNo() {
//        Object no = iRedisCache.get(ConstantsCache.getInstance().getReportNo());
//        iRedisCache.increment(ConstantsCache.getInstance().getReportNo(), 1);
//        if (no != null) {
//            int number = Integer.parseInt(String.valueOf(no)) + 1;
//            String zero = appendZero(7 - String.valueOf(number).length());
//            return zero + number;
//        }
        return "0000001";
    }

    /**
     * List转String
     *
     * @param targetList 目标集合
     * @return 返回转换后的集合信息
     */
    public static String listToString(List<?> targetList) {
        StringBuilder builder = new StringBuilder();
        if (targetList.size() != 0) {
            for (int i = 0; i < targetList.size(); i++) {
                builder.append(targetList.get(i));
                if (i != targetList.size() - 1) {
                    builder.append(",");
                }
            }
        }
        return builder.toString();
    }

    /**
     * 补0操作
     *
     * @param count 补0个数
     * @return 0组合字符
     */
    private static String appendZero(int count) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < count; i++) {
            builder.append("0");
        }
        return builder.toString();
    }

    /**
     * 是否为http(s)://开头
     *
     * @param link 链接
     * @return 结果
     */
    public static boolean isHttp(String link) {
        if (isNotNull(link)) {
            return link.startsWith(ConstantsOther.HTTP) || link.startsWith(ConstantsOther.HTTPS);
        }
        return false;
    }

    /**
     * 截取字符串
     *
     * @param str   字符串
     * @param start 开始
     * @param end   结束
     * @return 结果
     */
    public static String substring(final String str, int start, int end) {
        if (str == null) {
            return "";
        }
        if (end < 0) {
            end = str.length() + end;
        }
        if (start < 0) {
            start = str.length() + start;
        }
        if (end > str.length()) {
            end = str.length();
        }
        if (start > end) {
            return "";
        }
        if (start < 0) {
            start = 0;
        }
        if (end < 0) {
            end = 0;
        }

        return str.substring(start, end);
    }

    /**
     * 时间转换，可读
     *
     * @param minutes 分钟
     * @return 结果
     */
    public static String convertTime(int minutes) {
        int hours = minutes / 60;
        int remainingMinutes = minutes % 60;
        if (hours > 0) {
            if (remainingMinutes > 0) {
                return hours + "小时" + remainingMinutes + "分钟";
            } else {
                return hours + "小时";
            }
        } else {
            return remainingMinutes + "分钟";
        }
    }
}
